package com.jia.leetCode;

import java.util.HashMap;

/**
 * @program: Leetcode
 * @description: 290. 单词规律  https://leetcode-cn.com/problems/word-pattern/description/
 * @author: STU756
 * @create: 2020-08-22 14:23
 */
public class Pro0290 {
    public boolean wordPattern(String pattern, String str) {
        HashMap<Character, String> mapCS = new HashMap<Character, String>();
        HashMap<String, Character> mapSC = new HashMap<String, Character>();
        String[] strs = str.split(" ");
        if(pattern.length() != strs.length) return false;
        for(int i=0; i<pattern.length(); i++){
            char c = pattern.charAt(i);
            String s = strs[i];
            if(mapCS.containsKey(c)){
                if(!mapCS.get(c).equals(s)) return false;
            }else if(mapSC.containsKey(s)){
                if(!mapSC.get(s).equals(c)) return false;
            }else {
                mapCS.put(c, s);
                mapSC.put(s, c);
            }
        }
        return true;
    }
}
